package com.showtao.front.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.showtao.common.dto.ResultDto;
import com.showtao.common.dto.ResultDtoFactory;
import com.showtao.common.dto.widget.DataTablesRequestDto;
import com.showtao.common.dto.widget.DataTablesResponseDto;
import com.showtao.common.validation.ValidateException;
import com.showtao.front.domain.Message;
import com.showtao.front.domain.Tag;
import com.showtao.front.dto.ArticleDto;
import com.showtao.front.dto.CatalogDto;
import com.showtao.front.dto.upstream.ArticleSavingDto;
import com.showtao.front.dto.upstream.CatalogSavingDto;
import com.showtao.front.dto.upstream.TagSavingDto;
import com.showtao.front.service.ContentService;

@Controller
@RequiresPermissions (value={"content_admin"})
public class ContentController {

	@Autowired
	private ContentService contentService;

	@RequiresPermissions (value={"content_admin"})
	@RequestMapping(value = "/admin/tags", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponseDto<Tag> doSearchTags(
			@Valid @RequestBody DataTablesRequestDto requestDto,
			BindingResult result, HttpSession session) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		// 组装Pageable
		Pageable pageable = requestDto.toPagable();
		Page<Tag> page = contentService.findTags(pageable);
		DataTablesResponseDto<Tag> responseDto = new DataTablesResponseDto<Tag>();
		return responseDto.populateFromPage(page, requestDto.getDraw());
	}

	@RequestMapping(value = "/admin/tag", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doSaveTag(@Valid @RequestBody TagSavingDto dto,
			BindingResult result, HttpSession session) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		contentService.saveTag(dto);
		return ResultDtoFactory.toAck("标签创建成功");
	}

	@RequestMapping(value = "/admin/tag/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultDto doDeleteTag(@PathVariable("id") Long id,
			HttpSession session) throws Exception {
		contentService.deleteTag(id);
		return ResultDtoFactory.toAck("标签删除成功");
	}

	@RequestMapping(value = "/admin/catalogs", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponseDto<CatalogDto> doSearchCatalogs(
			@Valid @RequestBody DataTablesRequestDto requestDto,
			BindingResult result) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		// 组装Pageable
		Pageable pageable = requestDto.toPagable();
		Page<CatalogDto> page = contentService.findCatalogs(pageable);
		DataTablesResponseDto<CatalogDto> responseDto = new DataTablesResponseDto<CatalogDto>();
		return responseDto.populateFromPage(page, requestDto.getDraw());
	}

	@RequestMapping(value = "/admin/catalog", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doSaveCatalog(
	        @Valid @RequestBody CatalogSavingDto dto,
			BindingResult result) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		contentService.saveCatalog(dto);
		return ResultDtoFactory.toAck("目录创建成功");
	}

	@RequestMapping(value = "/admin/catalog/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultDto doDeleteCatalog(@PathVariable("id") Long id) throws Exception {
		contentService.deleteCatalog(id);
		return ResultDtoFactory.toAck("目录删除成功");
	}

	@RequestMapping(value = "/admin/articles", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponseDto<ArticleDto> doSearchArticles(
			@Valid @RequestBody DataTablesRequestDto requestDto,
			BindingResult result) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		// 组装Pageable
		Pageable pageable = requestDto.toPagable();
		Page<ArticleDto> page = contentService.findArticles(pageable);
		DataTablesResponseDto<ArticleDto> responseDto = new DataTablesResponseDto<ArticleDto>();
		return responseDto.populateFromPage(page, requestDto.getDraw());
	}

	@RequestMapping(value = "/admin/article", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto doSaveArticle(
	        @Valid @RequestBody ArticleSavingDto dto,
			BindingResult result) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		contentService.saveArticle(dto);
		return ResultDtoFactory.toAck("文章保存成功");
	}

	@RequestMapping(value = "/admin/article/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultDto doDeleteArticle(@PathVariable("id") Long id) throws Exception {
		contentService.deleteArticle(id);
		return ResultDtoFactory.toAck("文章删除成功");
	}
	
	@RequiresPermissions (value={"content_admin"})
	@RequestMapping(value = "/admin/messages", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponseDto<Message> doSearchMessages(
			@Valid @RequestBody DataTablesRequestDto requestDto,
			BindingResult result, HttpSession session) throws Exception {
		// 判断是否有验证错误
		if (result.hasErrors()) {
			throw new ValidateException(result);
		}
		// 组装Pageable
		Pageable pageable = requestDto.toPagable();
		Page<Message> page = contentService.findMessages(pageable);
		DataTablesResponseDto<Message> responseDto = new DataTablesResponseDto<Message>();
		return responseDto.populateFromPage(page, requestDto.getDraw());
	}
	
	@RequestMapping(value = "/admin/message/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultDto doDeleteMessage(@PathVariable("id") Long id) throws Exception {
		contentService.deleteMessage(id);
		return ResultDtoFactory.toAck("留言删除成功");
	}
}
